<template>
  <div class="ele-body">
    <el-form
      ref="searchForm"
      :model="table.where"
      label-width="120px"
      @keyup.enter.native="$refs.table.reload()"
      @submit.native.prevent
    >
      <el-row>
        <el-col :md="8" :sm="12">
          <el-form-item label="关键字:" prop="name">
            <el-input
              v-model="table.where.key"
              placeholder="请输入"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :md="8" :sm="12">
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              @click="$refs.table.reload({page:{page:1,limit:10}})"
            >查询
            </el-button>
            <el-button
              @click="(table.where = {}) && $refs.table.reload()"
            >重置
            </el-button>
            <el-button
              type="primary"
              plain
              @click="create"
            >添加域名
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <ele-data-table
      ref="table"
      :config="table"
      :choose.sync="choose"
      highlight-current-row
      height="calc(100vh - 300px)"
    >
      <template slot-scope="{}">
        <el-table-column
          prop="key"
          label="域名"
          show-overflow-tooltip
          min-width="110"
        />
        <el-table-column
          prop="manage"
          label="所属用户"
          show-overflow-tooltip
          min-width="110"
        >
          <template slot-scope="{ row }">
            {{ row.manage === 'None' ? '暂无':row.manage.name }}</template>
        </el-table-column>
        <el-table-column
          prop="start_date"
          label="生效时间"
          show-overflow-tooltip
          min-width="110"
        />
        <el-table-column
          prop="end_date"
          label="失效备注"
          show-overflow-tooltip
          min-width="110"
        />
        <el-table-column
          prop="description"
          label="域名备注"
          show-overflow-tooltip
          min-width="110"
        />
        <el-table-column
          label="操作"
          align="center"
          min-width="50"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-link
              icon="el-icon-edit"
              type="primary"
              :underline="false"
              @click="edit(scope.row)"
            >修改</el-link>
            <el-popconfirm
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
            >
              <el-link
                slot="reference"
                icon="el-icon-delete"
                type="primary"
                :underline="false"
                style="margin-left:5px"
              >删除</el-link>
            </el-popconfirm>
          </template>
        </el-table-column>
      </template>
    </ele-data-table>
    <el-dialog
      :title="editForm.id ? '更新信息' : '添加终端'"
      :visible.sync="isShow"
      :close-on-click-modal="false"
      width="600px"
      :destroy-on-close="true"
      :lock-scroll="false"
      @closed="editForm = {}"
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="editRules"
        label-width="100px"
        @submit.native.prevent
      >
        <el-form-item label="域名:" prop="key">
          <el-input
            v-model="editForm.key"
            placeholder="请输入域名"
            clearable
          />
        </el-form-item>
        <el-form-item label="所属用户:" prop="manage">
          <el-select
            v-model="editForm.manage"
            placeholder="请选择所属用户"
            style="width: 100%"
            filterable
            remote
            reserve-keyword
            :remote-method="romotePerson"
            :loading="managerLoading"
          >
            <el-option
              v-for="item in depList"
              :key="'manager' + item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="生效日期">
          <el-date-picker
            v-model="editForm.start_date"
            type="date"
            placeholder="选择生效日期"
            value-format="yyyy-MM-dd"
            style="width:100%"
          />
        </el-form-item>
        <el-form-item label="失效日期">
          <el-date-picker
            v-model="editForm.end_date"
            type="date"
            placeholder="选择失效日期"
            value-format="yyyy-MM-dd"
            style="width:100%"
          />
        </el-form-item>
        <el-form-item label="域名备注:">
          <el-input
            v-model="editForm.description"
            placeholder="请输入域名备注"
            :rows="4"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button
          type="primary"
          :loading="editLoading"
          @click="save"
        >保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, createDomain, delDomain, updateDomain } from '@/api/base'
export default {
  name: 'SystemVlan',
  data() {
    return {
      table: {
        url: '/base/list/domainlist/',
        where: {}
      },
      choose: [],
      editForm: {},
      isShow: false,
      editRules: {
        ip: [
          { required: true, message: '请输入IP地址', trigger: 'blur' }
        ],
        mac: [
          { required: true, message: '请输入MAC地址', trigger: 'blur' }
        ]
      },
      depList: [],
      managerLoading: false,
      editLoading: false,
      loading: false
    }
  },
  mounted() {
    this._getPersonList()
  },
  methods: {
    /* 创建终端 */
    create() {
      this.isShow = true
    },
    /* 员工列表远程搜索 */
    async romotePerson(query) {
      if (query !== '') {
        this.loading = true
        await this._getPersonList(query)
        this.loading = false
      } else {
        await this._getPersonList()
      }
    },
    /* 获得员工列表 */
    async _getPersonList(key) {
      getUserList(key).then(res => {
        if (res.data.data.length !== 0) {
          this.depList = res.data.data
        }
      })
    },
    /* 保存添加域名 */
    save() {
      this.$refs['editForm'].validate(valid => {
        if (valid) {
          this.editLoading = true
          if (this.editForm.id) {
            updateDomain(this.editForm).then(res => {
              const resp = res.data
              if (resp.code === 0) {
                this.$message({ type: 'success', message: '更新成功' })
                this.isShow = false
                this.$refs.table.reload()
              } else {
                this.$message({ type: 'error', message: resp.msg })
              }
            })
          } else {
            createDomain(this.editForm).then(res => {
              const resp = res.data
              if (resp.code === 0) {
                this.$message({ type: 'success', message: '保存成功' })
                this.isShow = false
                this.$refs.table.reload()
              } else {
                this.$message({ type: 'error', message: resp.msg })
              }
            })
          }
          this.editLoading = false
        } else {
          return false
        }
      })
    },
    /* 删除终端 */
    del(id) {
      delDomain(id).then(res => {
        if (res.data.code === 0) {
          this.$message({ type: 'success', message: '删除成功' })
          this.$refs.table.reload()
        } else {
          this.$message({ type: 'error', message: res.data.message })
        }
      })
    },
    /* 编辑终端信息 */
    edit(row) {
      this._getPersonList(row.manage.name).then(() => {
        this.editForm = Object.assign({}, {
          key: row.key,
          ip: row.ip,
          manage: row.manage.id,
          start_date: row.start_date,
          end_date: row.end_date,
          description: row.description
        })
        this.isShow = true
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
